package com.widget;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AbsoluteLayout;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;

public class apkSyncActivity extends Activity{

	AlertDialog.Builder alertBox;
	ArrayList<String> srcDirList; 
	ArrayList<String> pkgList,tempList; 
	EditText et; 
	LinearLayout ll; 
	ListView lv;
	List<PackageInfo> list;
	PackageManager pm;
	ArrayAdapter<String> adapter;
	ArrayList<String> searchResults;
	ArrayList<Integer> indexArray;
	private MSGHandler mMsgHandler;
	ManageProcess process;
	String path;
	String IP_ADDR;
	String USERNAME;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		process = new ManageProcess();
        process.acquireRoot();
        Bundle bundle = this.getIntent().getExtras();
		path = bundle.getString("PATH");
		IP_ADDR = bundle.getString("IPADDR");
		USERNAME = bundle.getString("USERNAME");
		alertBox=new AlertDialog.Builder(this);
		pm = getPackageManager();
		list = pm.getInstalledPackages(PackageManager.GET_ACTIVITIES);
		mMsgHandler = new MSGHandler(this);
		srcDirList = new ArrayList<String>();
		pkgList = new ArrayList<String>();
		indexArray = new ArrayList<Integer>();
		et = new EditText(this);
		ll = new LinearLayout(this);
		
		lv = new ListView(this);
		
		tempList = new ArrayList<String>();
		
		for(int i=0;i<list.size();i++){
			srcDirList.add(list.get(i).applicationInfo.sourceDir);
			pkgList.add(list.get(i).applicationInfo.packageName);
			tempList.add(list.get(i).applicationInfo.packageName);
			indexArray.add(i);
			Log.i("PackageName   ",pkgList.get(i));
		}
		
		
		AbsoluteLayout.LayoutParams etLayout = new AbsoluteLayout.LayoutParams(AbsoluteLayout.LayoutParams.FILL_PARENT,AbsoluteLayout.LayoutParams.WRAP_CONTENT,0,0);
		et.setHint("search apk");
		
		AbsoluteLayout.LayoutParams lvLayout = new AbsoluteLayout.LayoutParams(AbsoluteLayout.LayoutParams.WRAP_CONTENT,AbsoluteLayout.LayoutParams.WRAP_CONTENT,0,50);
		
		ll.setOrientation(LinearLayout.VERTICAL);
		
		
		adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,pkgList);
		lv.setAdapter(adapter);
		lv.setClickable(true);
		ll.addView(et, etLayout);
		ll.addView(lv, lvLayout);
		
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				
				
				alertBox.setTitle("Push Apk")
				.setMessage("Do you want to push this Application?")
				.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						final ProgressDialog dialog1 = ProgressDialog.show(apkSyncActivity.this, "Syncing...", 
								"Please wail while all applications are synchronized. This dialog will " +
								" automatically disappear once the sync is done.", true);
						mMsgHandler.setDialog(dialog1);
						ParseStreaming p = new ApkParser();
						p.endMatcher(1, mMsgHandler);
						process.setParser(p);
						String pathInCloud;
						String[]arr;
						pathInCloud=srcDirList.get(indexArray.get(arg2));
						
						arr=pathInCloud.split("/");
						for(int i=0;i<arr.length;i++)
							Log.i("array Info", arr[i]);
						if(pathInCloud.contains("mnt/asec"))
						{
							pathInCloud=arr[0]+"/"+arr[1]+"/"+arr[2]+"/"+arr[3]+"/";
						}
						else
						{
							pathInCloud=arr[0]+"/"+arr[1]+"/"+arr[2]+"/";
						}
						Log.i("!!!!!!! CHECK srcDirList !!!!!! ",pathInCloud);
						process.executeCmd(path+" -aP --delete --delete-excluded "+srcDirList.get(indexArray.get(arg2))+" rsync://"+USERNAME+"@"+IP_ADDR+"/"+USERNAME+"/backup"+pathInCloud);
						//Log.i("RSYNC",path+" -aP --delete --delete-excluded "+srcDirList.get(indexArray.get(arg2))+" rsync://"+USERNAME+"@"+IP_ADDR+"/"+USERNAME+"/backup/data/app/");
						//Log.i("Position", indexArray.get(arg2)+"");
						
						
					}
				})
				.setNegativeButton("No", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						
					}
				})
			.show();
			}
		});
		
		et.addTextChangedListener(new TextWatcher() {
			
			int typedTextLength;
			String typedText;
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				 
				typedText = et.getText().toString();
				typedTextLength = typedText.length();
				typedText = typedText.toUpperCase();
				adapter.clear();
				indexArray.clear();
				for(int i=0;i<tempList.size();i++){
					String temp = tempList.get(i).toUpperCase();
					if(typedTextLength < tempList.get(i).length()){
						if(temp.contains(typedText)){
							adapter.add(tempList.get(i));
							indexArray.add(i);
						}
					}
				}
				
				lv.setAdapter(adapter);
				
			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
		});	
		setContentView(ll);
	}
}
